<template>
  <div class="login-container">
    <el-form
      ref="myfrom"
      :model="from"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h2 class="title">后台登入系统</h2>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="from.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          v-model="from.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        ref="myform"
        :loading="loading"
        type="primary"
        style="width: 100%"
        @click.native.prevent="login"
        >登入</el-button
      >
    </el-form>
  </div>
</template>

<script>
//导入方法
// import { loginAPI } from "../../api/user.js";
import { mapActions } from "vuex";
export default {
  name: "Login",
  data() {
    return {
      // 用户信息
      from: {
        mobile: "13900000001",
        password: "123456",
      },
      // value 要校验的数据
      // callback 回调函数  如果校验通过，执行callback()  如果校验不通过，执行callback(new Error(''))
      loginRules: {
        // 校验手机号
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              reg.test(value)
                ? callback()
                : callback(new Error("请正确输入手机号"));
            },
            trigger: "change",
          },
        ],
        // 校验密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "密码的长度在6-16位之间 ",
            trigger: "blur",
          },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },

  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    ...mapActions({
      userlogin: "user/login",
    }),
    // 点击登入后
    login() {
      // 验证表单中的数据
      this.$refs.myfrom.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          // 验证通过 发送请求
          await this.userlogin(this.from);
          // 取消 loading
          this.loading = false;
          // 接收路由中的参数： _redirect
          const _redirect = this.$route.query._redirect;
          if (_redirect) {
            // 跳转到 _redirect 对应的路由
            this.$router.push("_redirect");
          } else {
            // 跳转到主页
            this.$router.push("/");
          }

          this.$message.success("登入成功");
          console.log("登入成功-------", valid);
        } else {
          console.log("登入失败");
          return false;
        }
      });

      //    this.$refs.myfrom.validate(async valid => {
      //      if (valid) {
      //          // 开启加载效果
      //          this.loading = true
      //          try {
      //              // // 验证通过
      //              await this.userlogin(this.form) // 发送网络请求
      //              // 跳转到主页
      //              this.$router.push('/')
      //          } catch (error) {
      //              console.log(error.message)
      //          } finally {
      //              // 取消 loading
      //              this.loading = false
      //          }
      //      } else {
      //          // 验证失败
      //          console.log('验证失败')
      //      }
      //  })
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url("../../assets/common/login.jpg");
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #fff;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 40px;
      color: $light_gray;
      margin: 0px auto 30px auto;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $light_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
